#
# Copyright 2009 Red Hat, Inc. and/or its affiliates.
#
# Licensed to you under the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.  See the files README and
# LICENSE_GPL_v2 which accompany this distribution.
#

from config import config

import sdc
import sd
import storage_exception as se


class StorageDomainFactory:
    storage_repository = config.get('irs', 'repository')
    __sdc = sdc.StorageDomainCache(storage_repository)


    @classmethod
    def produce(cls, sdUUID):
        """
        Produce a new Storage domain
        """

        newsd = cls.__sdc.lookup(sdUUID)
        if not newsd:
            raise se.StorageDomainDoesNotExist(sdUUID)
        return newsd


    @classmethod
    def create(cls, sdUUID, storageType, domainName, domClass, typeSpecificArg):
        """
        Create a new Storage domain
        """
        import fileSD
        import blockSD
        newsd = None
        if storageType in [sd.NFS_DOMAIN]:
            newsd = fileSD.FileStorageDomain.create(sdUUID=sdUUID,
                domainName=domainName, domClass=domClass,
                remotePath=typeSpecificArg, storageType=storageType)
        elif storageType in [sd.ISCSI_DOMAIN, sd.FCP_DOMAIN]:
            newsd = blockSD.BlockStorageDomain.create(sdUUID=sdUUID,
                domainName=domainName, domClass=domClass,
                vgUUID=typeSpecificArg, storageType=storageType)
        else:
            raise se.StorageDomainTypeError(storageType)
        cls.__sdc.addDomain(newsd)
        return newsd


    @classmethod
    def recycle(cls, sdUUID):
        """
        Cleanly destroys the domain
        """
        oldsd = cls.__sdc.lookup(sdUUID)
        if not oldsd:
            raise se.StorageDomainDoesNotExist(sdUUID)
        cls.__sdc.remDomain(oldsd)
        return oldsd.format()


    @classmethod
    def getAllDomains(cls):
        return cls.__sdc.getall()


    @classmethod
    def getAllUUIDs(cls):
        return cls.__sdc.getUUIDs()


    @classmethod
    def flushCache_deprecated(cls):
        cls.__sdc.flush_deprecated()


    @classmethod
    def refresh(cls):
        cls.__sdc.refresh()


    @classmethod
    def invalidateStorage(cls):
        cls.__sdc.invalidateStorage()


    @classmethod
    def refreshStorage(cls):
        cls.__sdc.refreshStorage()

